import { defineStore } from 'pinia';
import type { IChat, IChatFriend, IChatMessage } from '@/tool/lib/chat';
import { msgTip } from "./chatUtils";
import { user } from '@/store';
const chatStore = defineStore({
	// 这里的id必须为唯一ID
	id: 'chatStore',
	state: () : IChat => {
		return {
			loginState: 0, // 登录状态
			friendList: uni.getStorageSync('friendList') || [], //朋友列表,
			setAction: 0,
			activeName: '', // 当前聊天窗口人
			chatList: [],// 当前聊天窗口消息
			updateId: 0,
			tempFilePath: {} //临时文件
		}
	},
	// 等同于vuex的getter
	getters: {},
	// pinia 放弃了 mutations 只使用 actions
	actions: {
		// 接收消息
		async receiveMsg(msg : IChatMessage) {
			//TODO 系统消息处理
			if (msg.type! > 10) {
				//系统处理类型消息
				if (msg.type === 11) {
					console.log(msg)
					if (msg.from === this.activeName) {
						//单次确认
						for (let i = this.chatList.length - 1; i >= 0; i--) {
							if (this.chatList[i].id === msg.id) {
								this.chatList[i].status = 1//已读
								break
							}
						}
					}
				} else if (msg.type === 12) {
					//进入聊天窗口-全部确认已读
					if (msg.from === this.activeName) {
						for (const item of this.chatList) {
							item.status = 1//已读
						}
					}
				}
				return
			}
			//TODO 群组消息处理
			if (msg.code === 6) {
				//非本人消息才处理
				if (msg.from !== user?.userId) {
					// 群消息正在打开
					if (msg.to === this.activeName) {
						//当前聊天窗口打开的消息
						this.chatList.push(msg)
						this.updateId++;
						//发送消息确认群已读
						uni.sendSocketMessage({
							data: '@' + msg.to,
						});
					} else {
						msg.from = msg.to //设置来源为组id
						msg.avatar = msg.groupAvatar
						msg.nickName = msg.groupNickName
						msgCheckList(this, msg)
					}
				} else {
					//当前聊天窗口打开的消息
					if (msg.to === this.activeName) {
						this.chatList.push(msg)
						this.updateId++;
					}
				}
				return
			}
			//TODO 非群消息处理
			if (msg.to === this.activeName || msg.from === this.activeName) {
				//当前聊天窗口打开的消息
				this.chatList.push(msg)
				this.updateId++;

				//消息是否就是实时接收到页面
				if (msg.from === this.activeName) {
					//发送消息确认已读
					uni.sendSocketMessage({
						data: msg.id + ',' + msg.from,
					});
				}
			} else {
				//不在聊天窗口打开的消息且不是自己的消息
				if (msg.from !== user?.userId) {
					msgCheckList(this, msg)
				}
			}
		},
		// 设置当前窗口用户
		async setActive(userId : string) {
			if (user?.userId === userId) {
				uni.showToast({ title: '不能与自己聊天！' })
				return
			}
			this.activeName = userId
			this.setAction++
			// await setActive(state)
		},
		setFriendList(list : IChatFriend[]) {
			this.friendList = list
			uni.setStorage({
				key: 'friendList', data: list
			})
		}
	}
})

//检查消息是否存在朋友列表
const msgCheckList = (state : IChat, msg : IChatMessage) => {
	//提醒
	msgTip()
	//console.log(state.friendList)
	//修改内容
	let flag = true
	for (const item of state.friendList) {
		if (item.userId === msg.from) {
			item.content = msg.message!
			item.type = msg.type!
			item.count++
			flag = false
			break
		}
	}
	//说明列表不存在该消息，则直接插入
	if (flag) {
		let nickName = msg.nickName!
		let avatar = msg.avatar!
		let avatarShowId = msg.avatarShowId!
		if (msg.code === 7) {//客服消息-单独设置头像
			nickName = '系统客服'
			avatar = 'https://im.idb.com.cn/fs/static/customer.png'
			avatarShowId = ''
		}
		//插入
		state.friendList.unshift({
			userId: msg.userId,
			userName: msg.from!,
			nickName: nickName,
			avatarShowId: avatarShowId,
			photoPath: avatar,
			content: msg.message!, // 最近消息内容
			sendTime: msg.createTime!, // 最近消息时间
			type: msg.type!, // 最近一条消息类型
			count: 1,// 未读消息数目
			group: msg.code === 6 ? 1 : 0 //组类型
		})
	}
}
export default chatStore;